package com.yootk.listener;

import com.yootk.servlet.HelloServlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.ServletRegistration;
import jakarta.servlet.annotation.WebListener;

/**
 * 动态注册Servlet组件
 */
@WebListener
public class DynamicRegistrationServlet implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) { // 容器初始化进行组件注册
        // 所有的组件的动态注册都是基于ServletContext接口实例完成的
        ServletContext application = sce.getServletContext(); // 获取上下文接口实例
        ServletRegistration.Dynamic registration = application.addServlet("helloServlet", HelloServlet.class); // 反射类加载
        registration.setLoadOnStartup(1); // 设置Servlet启动时的加载
        registration.setInitParameter("message", "www.yootk.com");
        registration.addMapping("/hello.action", "/muyan.yootk", "/muyan/yootk/*");
    }
}
